﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using System.Data.SqlClient;

namespace vantai
{
    public partial class XemLichVanChuyen : Form
    {
        public XemLichVanChuyen()
        {
            InitializeComponent();
        }

        string sql=
"select ld.MaTuyen,t.TenTuyen,ld.MaXe,ld.NgayDi,ld.ThoiGianXuatPhat,ld.ThoiGianDuKienDen,ld.SLTaiXe,ld.SLPhuLai,ld.SLTaiXeDaDangKi,ld.SLPhuLaiDaDangKi from LichDi ld, Tuyen t where ld.MaTuyen=t.MaTuyen ";


        bool ischangeddatetime = false;// kiem tra datetimepicker thay doi

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
            
        }

        private void XemLichVanChuyenDirtyRead_MucSQLServer()
        {
            LichDiBUS ld = new LichDiBUS();
            DataTable dt = ld.DanhSachLichVanChuyen();
            int i = 1;
            dtgvdslichdi.Rows.Clear();
            foreach (DataRow r in dt.Rows)
            {
                string ngaydi = DateTime.Parse(r[3].ToString()).ToShortDateString();
                string giodi = DateTime.Parse(r[4].ToString()).ToShortTimeString();
                dtgvdslichdi.Rows.Add(i, r[0].ToString(), r[1].ToString(), r[2].ToString(), ngaydi, giodi, r[5].ToString(), r[6].ToString(), r[7].ToString(), r[8].ToString(), r[9].ToString());

                i++;
            }
            dtgvdslichdi.Columns[0].Width = 50;
            dtgvdslichdi.Columns[1].Width = 60;
            dtgvdslichdi.Columns[2].Width = 150;
            dtgvdslichdi.Columns[3].Width = 60;
            dtgvdslichdi.Columns[6].Width = 200;
            dtgvdslichdi.Columns[7].Width = 70;
            dtgvdslichdi.Columns[8].Width = 70;
            dtgvdslichdi.Columns[9].Width = 70;
            dtgvdslichdi.Columns[10].Width = 70;

            // dtgvdslichdi.ContextMenuStrip = dklichdi;

            for (int k = 0; k < dtgvdslichdi.RowCount; k++)
            {

                if (DateTime.Parse(dtgvdslichdi.Rows[k].Cells[4].Value.ToString()) <= DateTime.Now)
                {
                    dtgvdslichdi.Rows[k].DefaultCellStyle.BackColor = Color.Red;
                }
            }
        }

        private void XemLichVanChuyen_Load(object sender, EventArgs e)
        {
            if (Login.LoaiNV == 1)
            {
                toolStripMenuItem1.Visible = false;
                thoátToolStripMenuItem.Visible = false;
            }
            else
            {
                // gi tum lum vay may?! Ban truoc tao sua dau co thu nay?!
                cậpNhậtLịchĐiToolStripMenuItem.Visible = false;
            }
            XeBUS xb=new XeBUS();
            List<XeDTO> lstxe = xb.DanhSachXe();
            lstxe.Insert(0, new XeDTO(-1,"Chọn Xe Cần Xem"));
            cbxDanhSachXe.DataSource = lstxe;
            cbxDanhSachXe.DisplayMember = "Maxestring";
            cbxDanhSachXe.ValueMember = "maXe";


            TuyenBUS tb = new TuyenBUS();
            List<TuyenDTO> lsttuyen = tb.DanhSachTuyen();
            lsttuyen.Insert(0, new TuyenDTO(-1, "Chọn tuyến cần xem"));
            cbxDanhSachTuyen.DataSource = lsttuyen;
            cbxDanhSachTuyen.DisplayMember = "TenTuyen";
            cbxDanhSachTuyen.ValueMember = "MaTuyen";


            // xem lich van chuyen

            LichDiBUS ld=new LichDiBUS();
            DataTable dt = ld.DanhSachLichVanChuyen();
            int i = 1;
            foreach (DataRow r in dt.Rows)
            {
                string ngaydi=DateTime.Parse(r[3].ToString()).ToShortDateString();
                string giodi=DateTime.Parse(r[4].ToString()).ToShortTimeString();
                dtgvdslichdi.Rows.Add(i, r[0].ToString(), r[1].ToString(), r[2].ToString(), ngaydi, giodi, r[5].ToString(), r[6].ToString(), r[7].ToString(), r[8].ToString(), r[9].ToString());
              
                i++;
            }
            dtgvdslichdi.Columns[0].Width = 50;
            dtgvdslichdi.Columns[1].Width = 60;
            dtgvdslichdi.Columns[2].Width = 150;
            dtgvdslichdi.Columns[3].Width = 60;
            dtgvdslichdi.Columns[6].Width = 200;
            dtgvdslichdi.Columns[7].Width = 70;
            dtgvdslichdi.Columns[8].Width = 70;
            dtgvdslichdi.Columns[9].Width = 70;
            dtgvdslichdi.Columns[10].Width = 70;

           // dtgvdslichdi.ContextMenuStrip = dklichdi;

            for (int k = 0; k < dtgvdslichdi.RowCount; k++)
            {

                if (DateTime.Parse(dtgvdslichdi.Rows[k].Cells[4].Value.ToString()) <= DateTime.Now)
                {
                    dtgvdslichdi.Rows[k].DefaultCellStyle.BackColor = Color.Red;
                }
            }


        }

        private void dtgvdslichdi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //toolTip1.Show("tip", this, new Point(200, 100));
        }
        int rowindex = -1;
        private void dtgvdslichdi_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            //if (e.Button == MouseButtons.Right)
            //{
                //dklichdi.Show(dtgvdslichdi,e.X,e.Y);
                rowindex = e.RowIndex;
                //MessageBox.Show(rowindex.ToString());
            //}
        }

        private void thoátToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (rowindex != -1)
            {
                if (MessageBox.Show("Bạn có chắc chắn muốn đăng ký tham gia vào lịch đi này không?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    DangKyLichDiDTO dkld = new DangKyLichDiDTO();
                    dkld.MaNV = Login.MaNV;// lay manv tu form dang nhap
                    dkld.MaTuyen = int.Parse(dtgvdslichdi.Rows[rowindex].Cells[1].Value.ToString());
                    dkld.MaXe = int.Parse(dtgvdslichdi.Rows[rowindex].Cells[3].Value.ToString());
                    dkld.NgayDi = DateTime.Parse(dtgvdslichdi.Rows[rowindex].Cells[4].Value.ToString());
                    DangKyLichDiBUS dkb = new DangKyLichDiBUS();
                    try
                    {
                        if (dkb.DangKyLichDi(dkld))
                        {
                            dkb.themslDangKyLichDi(dkld);
                            MessageBox.Show("Đăng ký thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            LoadDanhSachLichDi(sql);
                        }

                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                }
            }
            else
            {
                MessageBox.Show("Vui lòng chọn vào một lịch muốn đăng ký", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void dtgvdslichdi_MouseHover(object sender, EventArgs e)
        {
           

        }

        private void dtgvdslichdi_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
          
        }

        private void LoadDanhSachLichDi(string sql)
        {
            LichDiBUS ldb=new LichDiBUS();
            DataTable dt = ldb.DanhSachLichDi(sql);
            dtgvdslichdi.Rows.Clear();
            int i = 1;
            foreach (DataRow r in dt.Rows)
            {
                string ngaydi = DateTime.Parse(r[3].ToString()).ToShortDateString();
                string giodi = DateTime.Parse(r[4].ToString()).ToShortTimeString();
                dtgvdslichdi.Rows.Add(i, r[0].ToString(), r[1].ToString(), r[2].ToString(), ngaydi, giodi, r[5].ToString(), r[6].ToString(), r[7].ToString(),r[8].ToString(),r[9].ToString());

                i++;
            }
            dtgvdslichdi.Columns[0].Width = 50;
            dtgvdslichdi.Columns[1].Width = 60;
            dtgvdslichdi.Columns[2].Width = 150;
            dtgvdslichdi.Columns[3].Width = 60;
            dtgvdslichdi.Columns[6].Width = 200;
            dtgvdslichdi.Columns[7].Width = 70;
            dtgvdslichdi.Columns[8].Width = 70;
            for (int k = 0; k < dtgvdslichdi.RowCount; k++)
            {

                if (DateTime.Parse(dtgvdslichdi.Rows[k].Cells[4].Value.ToString()) <= DateTime.Now)
                {
                    dtgvdslichdi.Rows[k].DefaultCellStyle.BackColor = Color.Red;
                }
            }

        }
        private void cbxDanhSachTuyen_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxDanhSachTuyen.SelectedIndex != 0)
            {
                string sql2 = sql;
                int mtuyen = Int32.Parse(cbxDanhSachTuyen.SelectedValue.ToString());
                sql2 += " and t.MaTuyen=" + mtuyen;
                if (cbxDanhSachXe.SelectedIndex != 0)
                {
                    int mxe = Int32.Parse(cbxDanhSachXe.SelectedValue.ToString());
                    sql2 += " and ld.MaXe=" + mxe;
                }
                if (ischangeddatetime)
                {
                    sql2 += " and ld.NgayDi='" + dtpNgayDi.Value.ToShortDateString() + "'";
                }
                LoadDanhSachLichDi(sql2);
            }// truong hop nguoc lai ?
            
        }
      //  bool x = false;
      //  int mxe = -1;
        private void cbxDanhSachXe_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxDanhSachXe.SelectedIndex != 0)
            {
                string sql2=sql;
                int mxe = Int32.Parse(cbxDanhSachXe.SelectedValue.ToString());
                sql2 += " and ld.MaXe=" + mxe;
                if (cbxDanhSachTuyen.SelectedIndex != 0)
                {
                    int mtuyen = Int32.Parse(cbxDanhSachTuyen.SelectedValue.ToString());
                    sql2 += " and t.MaTuyen=" + mtuyen;
                }
                if (ischangeddatetime)
                {
                    sql2 += " and ld.NgayDi='" + dtpNgayDi.Value.ToShortDateString() + "'";
                }
                LoadDanhSachLichDi(sql2);
           
            }// truong hop nguoc lai ?
            

        }
        
        private void dtpNgayDi_ValueChanged(object sender, EventArgs e)
        {
            ischangeddatetime = true;
            string sql2 = sql;
            sql2 += " and ld.NgayDi='" + dtpNgayDi.Value.ToShortDateString() + "'";
            if (cbxDanhSachTuyen.SelectedIndex != 0)
            {
                int mtuyen = Int32.Parse(cbxDanhSachTuyen.SelectedValue.ToString());
                sql2 += " and t.MaTuyen=" + mtuyen;
            }
            if (cbxDanhSachXe.SelectedIndex != 0)
            {
                int mxe = Int32.Parse(cbxDanhSachXe.SelectedValue.ToString());
                sql2 += " and ld.MaXe=" + mxe;
            }
            LoadDanhSachLichDi(sql2);

            //ischangeddatetime = false;

            //string sql2 = sql;
            //if (t == true)
            //{
            //    sql2 += " and t.MaTuyen=" + matuyen;
            //}
            //if (x == true)
            //{
            //    sql2 += " and ld.MaXe=" + mxe;
            //}
            //sql2+=" and ld.NgayDi='"+dtpNgayDi.Value.ToShortDateString()+"'";
            //LoadDanhSachLichDi(sql2);
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }
        private void XemLichVanChuyenVoiDirtyRead()
        {
            DataTable dt=new DataTable();
            SqlTransaction transaction = null;
             string constring=Login.constringFromLogin;
             SqlConnection connection=new SqlConnection(constring);
            try
            {
                connection.Open();
                SqlCommand cmd=connection.CreateCommand();
                //của tao lun
                if (Login.KT == true)
                {
                    transaction = connection.BeginTransaction(IsolationLevel.ReadUncommitted);
                }
                else
                {
                    transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);
                }

                // thanh
               // transaction = connection.BeginTransaction();
                string sqlthanh = "select ld.MaTuyen,t.TenTuyen,ld.MaXe,ld.NgayDi,ld.ThoiGianXuatPhat,ld.ThoiGianDuKienDen,ld.SLTaiXe,ld.SLPhuLai,ld.SLTaiXeDaDangKi,ld.SLPhuLaiDaDangKi from LichDi ld with(nolock), Tuyen t with(nolock) where ld.MaTuyen=t.MaTuyen "; 
                //--end thanh---------------
                
                //kết thúc của tao
                cmd.Connection=connection;
                cmd.Transaction=transaction;
                cmd.CommandType=CommandType.Text;
                // thanh
                cmd.CommandText = sql;
                //------end thanh-----------
               // cmd.CommandText=sql;
                SqlDataAdapter da=new SqlDataAdapter(cmd);
                da.Fill(dt);

                transaction.Commit();


                // hien thi len luoi 
                int i=1;
                dtgvdslichdi.Rows.Clear();
                foreach (DataRow r in dt.Rows)
            {
                string ngaydi = DateTime.Parse(r[3].ToString()).ToShortDateString();
                string giodi = DateTime.Parse(r[4].ToString()).ToShortTimeString();
                dtgvdslichdi.Rows.Add(i, r[0].ToString(), r[1].ToString(), r[2].ToString(), ngaydi, giodi, r[5].ToString(), r[6].ToString(), r[7].ToString(),r[8].ToString(),r[9].ToString());

                i++;
            }
            dtgvdslichdi.Columns[0].Width = 50;
            dtgvdslichdi.Columns[1].Width = 60;
            dtgvdslichdi.Columns[2].Width = 150;
            dtgvdslichdi.Columns[3].Width = 60;
            dtgvdslichdi.Columns[6].Width = 200;
            dtgvdslichdi.Columns[7].Width = 70;
            dtgvdslichdi.Columns[8].Width = 70;
            for (int k = 0; k < dtgvdslichdi.RowCount; k++)
            {

                if (DateTime.Parse(dtgvdslichdi.Rows[k].Cells[4].Value.ToString()) <= DateTime.Now)
                {
                    dtgvdslichdi.Rows[k].DefaultCellStyle.BackColor = Color.Red;
                }
            }

                


            }catch(Exception ex){
                MessageBox.Show(ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //LoadDanhSachLichDi(sql);
            //cbxDanhSachTuyen.SelectedIndex = 0;
            //cbxDanhSachXe.SelectedIndex = 0;
            
            // Thanh
            //XemLichVanChuyenDirtyRead_MucSQLServer();
            
            
            // Nha
            XemLichVanChuyenVoiDirtyRead();
        }
        public static int smatuyen;
        public static string stentuyen;
        public static int smaxe;
        public static DateTime sngaydi;
        public static DateTime sgiodi;
        public static DateTime sgioden;
        public static int ssltaixe;
        public static int sslphulai;
        public static int ssltaixedangki;
        public static int sslphulaidangki;
        private void cậpNhậtLịchĐiToolStripMenuItem_Click(object sender, EventArgs e)
        {


        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void cậpNhậtLịchĐiToolStripMenuItem_Click_1(object sender, EventArgs e)
        {

            if (MessageBox.Show("Bạn có chắc chắn muốn cập nhật lịch đi này không?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                if (DateTime.Parse(dtgvdslichdi.CurrentRow.Cells[4].Value.ToString()) <= DateTime.Now)
                {
                    MessageBox.Show("Tuyến này xe đã đi không được cập nhật");
                }
                else
                {
                    smatuyen = Int32.Parse(dtgvdslichdi.CurrentRow.Cells[1].Value.ToString());
                    stentuyen = dtgvdslichdi.CurrentRow.Cells[2].Value.ToString();
                    smaxe = Int32.Parse(dtgvdslichdi.CurrentRow.Cells[3].Value.ToString());
                    sngaydi = DateTime.Parse(dtgvdslichdi.CurrentRow.Cells[4].Value.ToString());
                    sgiodi = DateTime.Parse(dtgvdslichdi.CurrentRow.Cells[5].Value.ToString());
                    sgioden = DateTime.Parse(dtgvdslichdi.CurrentRow.Cells[6].Value.ToString());
                    ssltaixe = Int32.Parse(dtgvdslichdi.CurrentRow.Cells[7].Value.ToString());
                    sslphulai = Int32.Parse(dtgvdslichdi.CurrentRow.Cells[8].Value.ToString());
                    ssltaixedangki = Int32.Parse(dtgvdslichdi.CurrentRow.Cells[9].Value.ToString());
                    sslphulaidangki = Int32.Parse(dtgvdslichdi.CurrentRow.Cells[10].Value.ToString());
                    Sualichdi a = new Sualichdi();
                    a.ShowDialog();
                    LoadDanhSachLichDi(sql);
                }

            }
        }
    }
}
